<?php
namespace app\controller;

use think\Request;
use app\model\NavSiteType;
use app\model\User as UserModel;
use app\common\UtilService;
use app\validate\UserValidate;
use think\helper\Str;

class User extends Base
{
	/**
	 * [FunctionName 用户中心]
	 * @since  2022-03-21
	 * @author Dai哥
	 * @param  Request    $request [description]
	 */
	public function index(Request $request)
	{
		$param = UtilService::getMore([
            ['id', $request->userId],
        ]);

        $userInfo = UserModel::getUserInfo($param);
        // dump($userInfo);exit;

		return view('index', compact('userInfo'));
	}

	/**
	 * [profile 修改资料]
	 * @since  2022-03-21
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function profile(Request $request)
	{
		if ($request->isAjax()) {
			try {

				$param = UtilService::postMore([
		            ['avatar', ''],
		            ['descs', ''],
		            ['gender', ''],
		            ['nickname', ''],
		            ['school', ''],
		            ['major', ''],
		            ['entrance', ''],
		            ['education', ''],
		            ['region', ''],
		            ['update_time', time()],
		        ]);

		        validate(UserValidate::class)->scene('update')->check($param);

		        $res = UserModel::where('id', $request->userId)->update($param);
		        
		        if($res){

		        	session('zbdInfo.nickname', $param['nickname']);
		        	session('zbdInfo.avatar', $param['avatar']);

		        	return success($res, '更新资料成功');
		        }

		        return error('更新资料失败');

	        } catch (\Exception $e) {

	            // 这是进行异常捕获
	            return error($e->getMessage());
	        }
		}

		$userInfo = UserModel::getUserInfo(['id' => $request->userId]);

		return view('profile', compact('userInfo'));
	}

	/**
	 * [auth 认证身份]
	 * @since  2022-03-21
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function auth(Request $request)
	{
		if ($request->isAjax()) {
			try {

				$param = UtilService::postMore([
		            ['name', ''],
		            ['phone', ''],
		            ['auth_type', '1'],
		            ['update_time', time()],
		        ]);

		        validate(UserValidate::class)->scene('auth')->check($param);

		        $res = UserModel::where('id', $request->userId)->update($param);
		        
		        if($res){
		        	return success($res, '认证成功');
		        }

		        return error('认证失败');

	        } catch (\Exception $e) {

	            // 这是进行异常捕获
	            return error($e->getMessage());
	        }
		}

		$userInfo = UserModel::getUserInfo(['id' => $request->userId]);

		return view('auth', compact('userInfo'));
	}

	/**
	 * [open 生成应用密钥]
	 * @since  2022-03-21
	 * @author Dai哥
	 * @param  Request    $request [description]
	 * @return [type]              [description]
	 */
	public function open(Request $request)
	{
		$userInfo = UserModel::find($request->userId);

		if ($request->param('type')) {

				$app_id = get_guid_v4();
				$arr = array(
					'auth_app_secret' => Str::upper(get_guid_v4()),
					'auth_app_key' 	  => Str::random($length = 16),
					'update_time'     => time(),
				);

				$userInfo->auth_app_secret = Str::upper(get_guid_v4());
				$userInfo->auth_app_key 	   = Str::random($length = 16);
				$userInfo->update_time     = time();
				$userInfo->save();

				return redirect('/user/open');
		}


		return view('open', compact('userInfo'));
	}

	/**
	 * [privacy 隐私设置]
	 * @param  Request $request [description]
	 * @return [type]           [description]
	 */
	public function privacy(Request $request)
	{
		if ($request->isAjax()) {
			try {

				$param = UtilService::postMore([
		            ['key', ''],
		            ['update_time', time()],
		        ]);
				
				$arr['update_time'] = time();

		        $res = UserModel::where('id', $request->userId)->field('id, is_jiaoyu, is_jichu, is_guanzhu, is_fans')->find();
		        if(!$res){
		        	return error('用户不存在');
		        }

		        switch ($param['key']) {
		        	case 'is_jiaoyu':
		        		if($res['is_jiaoyu'] == 1 ){
		        			$res->is_jiaoyu = 0;
		        			$msg = '已关闭';
		        		} else{
		        			$res->is_jiaoyu = 1;
		        			$msg = '开启成功';
		        		}
		        		break;
		        	
		        	case 'is_jichu':
		        		if($res['is_jichu'] == 1 ){
		        			$res->is_jichu = 0;
		        			$msg = '已关闭';
		        		} else{
		        			$res->is_jichu = 1;
		        			$msg = '开启成功';
		        		}
		        		break;

		        	case 'is_guanzhu':
		        		if($res['is_guanzhu'] == 1 ){
		        			$res->is_guanzhu = 0;
		        			$msg = '已关闭';
		        		} else{
		        			$res->is_guanzhu = 1;
		        			$msg = '开启成功';
		        		}
		        		break;

		        	case 'is_fans':
		        		if($res['is_fans'] == 1 ){
		        			$res->is_fans = 0;
		        			$msg = '已关闭';
		        		} else{
		        			$res->is_fans = 1;
		        			$msg = '开启成功';
		        		}
		        		break;

		        	default:
		        		return error('没有该设置');
		        		break;
		        }

		        $res->update_time = time();
		        $res->save();
		        
		        return success($res, $msg);

	        } catch (\Exception $e) {

	            // 这是进行异常捕获
	            return error($e->getMessage());
	        }
		}

		$userInfo = UserModel::getUserInfo(['id' => $request->userId]);

		// dump($userInfo);exit;

		return view('privacy', compact('userInfo'));
	}
}